<template>
  <el-dialog
    :before-close="dialogClose"
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="95px">
      <el-row >
        <el-col :span="12">
          <el-form-item label="承运商名称" prop="orgName">
            <el-input v-model="dataForm.orgName" placeholder="承运商名称"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="类型" prop="orgCategory">
            <dictionary-select dictType="org_category" :dictionaryCode.sync="dataForm.orgCategory" :dictionaryName.sync="dataForm.orgCategoryName"></dictionary-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="税号" prop="taxNo">
            <el-input v-model="dataForm.taxNo" placeholder="税号"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="开户行" prop="hplBank">
            <el-input v-model="dataForm.hplBank" placeholder="开户行"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="银行账户" prop="bankNo">
            <el-input v-model="dataForm.bankNo" placeholder="银行账户"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="联系人" prop="contactPerson">
            <el-input v-model="dataForm.contactPerson" placeholder="联系人"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="联系电话" prop="contactTel">
            <el-input v-model="dataForm.contactTel" placeholder="联系电话"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="地址" prop="areaCodeList" >
            <el-row>
              <el-col :span="12">
                <area-select :areaCodeList.sync="dataForm.areaCodeList" :areaListName.sync="areaListName" ref="areaList"></area-select>
              </el-col>
              <el-col :span="12">
                <el-input v-model="dataForm.hplAddr" placeholder="详细地址"></el-input>
              </el-col>
            </el-row>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="备注信息" prop="remarks">
            <el-input v-model="dataForm.remarks" type="textarea" placeholder="备注信息"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="图片" prop="pic">
            <el-input v-model="dataForm.pic" placeholder="图片"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="dialogClose">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  import dictionarySelect from '@/components/common/dictionary-select'
  import areaSelect from '@/components/common/area-select'
  import { isMobile } from '@/utils/validate'
  import { getCaption } from '@/utils/index'
  var validateMobile = (rule, value, callback) => {
    if (!isMobile(value) && value) {
      callback(new Error('手机号格式错误'))
    } else {
      callback()
    }
  }
  var validateArea = (rule, value, callback) => {
    if (value) {
      if (value.length !== 3) {
        callback(new Error('详细地址不能为空'))
      } else {
        callback()
      }
    } else {
      callback()
    }
  }
  const defaultForm = {
    orgCategoryName: '',
    orgCategory: '',
    orgCode: '',
    orgName: '',
    taxNo: '',
    hplBank: '',
    bankNo: '',
    contactPerson: '',
    contactTel: '',
    areaCodeList: [],
    hplAddr: '',
    remarks: '',
    pic: ''
  }
  export default {
    components: {dictionarySelect, areaSelect},
    data () {
      return {
        visible: false,
        dataForm: JSON.parse(JSON.stringify(defaultForm)),
        isAdd: true,
        dataRule: {
          orgName: [
            { required: true, message: '承运商名称不能为空', trigger: 'blur' }
          ],
          orgCategory: [
            { required: true, message: '请选择税号', trigger: 'blur' }
          ],
          contactTel: [
            { validator: validateMobile, trigger: 'blur' }
          ],
          areaCodeList: [
            { validator: validateArea, message: '请选择省市区地址', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      // 永久日期切换项
      flagChange (data) {
        if (data === '0' || data === '1') {
          const flag = 0
          data = flag ? this.dataForm.endDate : this.dataForm.endDate = ''
        } else {
          this.dataForm.flag = 0
        }
      },
      init (id) {
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          this.isAdd = true
          if (id) {
            this.$http({
              url: this.$http.adornUrl(`${this.$API.BASE_INFO.CARRIERPER.INFO}/${id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              this.isAdd = false
              this.dataForm = data
              this.$refs.areaList.areaDetail(data.areaCodeList)
              if (this.dataForm.hplAddr) {
                this.dataForm.hplAddr = (getCaption(this.dataForm.hplAddr, ':'))
              }
            })
          } else {
            this.dataForm = JSON.parse(JSON.stringify(defaultForm))
            this.$refs.areaList.init()
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`${this.isAdd ? this.$API.BASE_INFO.CARRIERPER.SAVE : this.$API.BASE_INFO.CARRIERPER.UPDATE}`),
              method: 'post',
              data: this.$http.adornData(this.dataForm)
            }).then(({data}) => {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.visible = false
                  this.$emit('refreshDataList')
                }
              })
            })
          }
        })
      },
      dialogClose () {
        this.visible = false
      }
    }
  }
</script>
